/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.common.session.stateless;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import merlion.common.entity.JobToDo;
import merlion.common.entity.Staff;
import merlion.common.entity.SystemUserAccount;
import merlion.common.util.Consts;
import merlion.crm.sopm.entity.SalesOrder;
import merlion.crm.sopm.entity.SalesQuotation;
import merlion.scm.lcm.entity.DeliveryOrder;

/**
 *
 * @author Zhang Ying
 */
@Stateless
public class JobListSessionBean implements JobListSessionBeanLocal {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public SalesOrder getRelatedSalesOrder(Long relatedObjectId) {
        return entityManager.find(SalesOrder.class, relatedObjectId);
    }
    
    @Override
    public DeliveryOrder getRelatedDeliveryOrder(Long relatedObjectId) {
        return entityManager.find(DeliveryOrder.class, relatedObjectId);
    }

    @Override
    public SalesQuotation getRelatedSalesQuotation(Long relatedObjectId) {
        return entityManager.find(SalesQuotation.class, relatedObjectId);
    }

    @Override
    public Staff getUpdatedStaff(Long id) {
        return entityManager.find(Staff.class, id);
    }

    @Override
    public void addNewJob(Long accId, JobToDo jobTD) {
        try {
            SystemUserAccount sua = entityManager.find(SystemUserAccount.class, accId);
            sua.getJobToDos().add(jobTD);
            entityManager.merge(sua);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public Collection<JobToDo> getAllMyJobs(Long accId) {
        SystemUserAccount sua = entityManager.find(SystemUserAccount.class, accId);
        return sua.getJobToDos();
    }

    @Override
    public JobToDo getJobToDo(Long id) {
        return entityManager.find(JobToDo.class, id);
    }

    @Override
    public boolean markJobStatusAsDone(Long id) {
        try {
            JobToDo jtd = entityManager.find(JobToDo.class, id);
            jtd.setStatus(Consts.JOB_STATUS_FINISHED);
            entityManager.merge(jtd);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public Collection<JobToDo> getJobsFromDep(String department) {
        // TODO: may update department
        if (department.equals(Consts.FIN_DEP)) {
            Query query = entityManager.createQuery("SELECT j FROM JobToDo j WHERE j.jobType=:p1 OR j.jobType=:p2 OR j.jobType=:p3");
            query.setParameter("p1", Consts.JOB_TYPE_CC);
            query.setParameter("p2", Consts.JOB_TYPE_INVOICE_CC);
            query.setParameter("p3", Consts.JOB_TYPE_INVOICE_CIA);
            return (List<JobToDo>) query.getResultList();
        } else if (department.equals(Consts.SALES_DEP)) {
            Query query = entityManager.createQuery("SELECT j FROM JobToDo j WHERE j.jobType=:p1 OR j.jobType=:p2");
            query.setParameter("p1", Consts.JOB_TYPE_ATP_SO);
            query.setParameter("p2", Consts.JOB_TYPE_ATP_SQ);
            return (List<JobToDo>) query.getResultList();
        } else if (department.equals(Consts.MRP_DEP)) {
            Query query = entityManager.createQuery("SELECT j FROM JobToDo j WHERE j.jobType=:p1 OR j.jobType=:p2");
            query.setParameter("p1", Consts.JOB_TYPE_SIMULATE_PENDING_SQ);
            query.setParameter("p2", Consts.JOB_TYPE_SIMULATE_PENDING_SO);
            return (List<JobToDo>) query.getResultList();
        }
        return null;
    }

    @Override
    public Staff getStaffByJobId(Long id) {
        JobToDo jtd = entityManager.find(JobToDo.class, id);
        Query query = entityManager.createQuery("SELECT s FROM Staff s");
        Collection<Staff> staffs = (List<Staff>) query.getResultList();
        for (Staff s: staffs){
            if (s.getSystemUserAccount().getJobToDos().contains(jtd)){
                return s;
            }
        }
        return null;
    }

    @Override
    public boolean haveThisJobAlready(String jobType, Long relatedObjectId) {
        Query query = entityManager.createQuery("SELECT j FROM JobToDo j WHERE j.jobType=:p1 AND j.relatedId=:p2");
        query.setParameter("p1", jobType);
        query.setParameter("p2", relatedObjectId);
        if (query.getResultList().isEmpty()){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public Collection<JobToDo> getAllMyPendingJobsDESC(Long accId) {
        SystemUserAccount sua = entityManager.find(SystemUserAccount.class, accId);
        List<JobToDo> lists = (List<JobToDo>) sua.getJobToDos();
        Collection<JobToDo> result = new ArrayList<JobToDo>();
        for (int i = lists.size()-1 ; i >=0; i--){
            JobToDo jtd = lists.get(i);
            if (jtd.getStatus().equals(Consts.JOB_STATUS_PENDING)){
                result.add(jtd);
            }
        }
        return result;
    }
}
